const Base = require("./base");
module.exports = class MeritService extends Base {
  getMantra(query = {}) {
    const { uid, type } = query;
    const expr = [];
    if (type) {
      expr.push(`type='${type}'`);
    }
    if (uid) {
      expr.push(`uid='${uid}'`);
    } else {
      expr.push("uid IS NULL");
    }
    const db = this.app.mysql.use("foindia");
    return db
      .query(
        `SELECT id,\`type\`,title,show_title,content,isplus,font_size,font_family,pay_type,price FROM mantra WHERE ${expr.join(
          " AND "
        )};`
      )
      .then((res) => res[0]);
  }
  getUserMantraCount(uid) {
    const db = this.app.mysql.use("foindia");
    return db
      .query(`SELECT COUNT(id) AS count FROM mantra WHERE uid='${uid}';`)
      .then((res) => res[0]);
  }
  postMantra(params) {
    const { keys, replacements, values } = this.installParams(params);
    return this.app.mysql
      .use("foindia")
      .query(`INSERT INTO mantra(${keys}) VALUES(${values});`, {
        replacements,
      })
      .then((res) => res[0]);
  }
  putMantra(params) {
    const { id } = params;
    delete params.id;
    const { replacements, values } = this.installParams(params, 2);
    return this.app.mysql
      .use("foindia")
      .query(`UPDATE mantra SET${values} WHERE id='${id}'`, {
        replacements,
      });
  }
};
